package com.example.protestgps;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteforUserID extends SQLiteOpenHelper{
	
	// Database Version
		private static final int DATABASE_VERSION = 1;

		// Database Name
		private static final String DATABASE_NAME = "mydatabase";

		// Table Name
		private static final String TABLE_USERID = "user";

		

	public SQLiteforUserID(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		db.execSQL("CREATE TABLE IF NOT EXISTS user (ID INTEGER PRIMARY KEY," + "USERID TEXT);");
	
		System.out.println("Create");
		Log.d("CREATE TABLE", "Create Table Successfully.");

		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	
	// Insert Data
		public long InsertData1(int id,String userid) {
			// TODO Auto-generated method stub

			try {
				SQLiteDatabase db;
				db = this.getWritableDatabase(); // Write Data
				ContentValues Val = new ContentValues();
				Val.put("ID",id);
				Val.put("USERID", userid);
				long rows = db.insert(TABLE_USERID, null, Val);
				Log.d("Val", "" + Val);

				db.close();
				return rows; // return rows inserted.

			} catch (Exception e) {
				return -1;
			}

		}

		
		public List<UserIDMember> SelectData() {
			// TODO Auto-generated method stub
			
			 try {
				 List<UserIDMember> MemberUser = new ArrayList<UserIDMember>();
				 
				 SQLiteDatabase db;
				 db = this.getReadableDatabase(); // Read Data
					
				 String strSQL = "SELECT USERID FROM user";
				 Cursor cursor = db.rawQuery(strSQL, null);
				 
				 	if(cursor != null)
				 	{
				 	    if (cursor.moveToFirst()) {
				 	        do {
				 	        	UserIDMember member = new UserIDMember();
				 	        	member.UserID(cursor.getString(0));
				 	        	Log.d("cursor", "");
				 	        	MemberUser.add(member);
				 	        } while (cursor.moveToNext());
				 	    }
				 	}
				 	
				 	cursor.close();
					db.close();
					Log.d("MemberUser", ""+MemberUser);
					return MemberUser;
					
					
			 } catch (Exception e) {
			    return null;
			 }

		}
}
